package com.clw.classfile.attribute;

import com.clw.classfile.BytesReader;
import com.clw.classfile.attribute.info.AttributeInfo;
import com.clw.classfile.attribute.info.Code;
import com.clw.classfile.attribute.info.ConstantValue;
import com.clw.classfile.attribute.info.Deprecated;
import com.clw.classfile.attribute.info.Exceptions;
import com.clw.classfile.attribute.info.InnerClasses;
import com.clw.classfile.attribute.info.LineNumberTable;
import com.clw.classfile.attribute.info.LocalVariableTable;
import com.clw.classfile.attribute.info.RuntimeVisibleAnnotations;
import com.clw.classfile.attribute.info.Signature;
import com.clw.classfile.attribute.info.SourceFile;
import com.clw.classfile.attribute.info.StackMapTable;
import com.clw.classfile.attribute.info.Synthetic;
import com.clw.classfile.attribute.info.Unparsed;
import com.clw.classfile.constantpool.ConstantPool;

/**
 * Created by chengliwei on 2018/11/12.
 */
public class Attribute {

    public static AttributeInfo readAttribute(BytesReader reader, ConstantPool constantPool) {
        short attributeNameIndex = reader.readShort();
        String attributeName = constantPool.getUtf8(attributeNameIndex);
        switch (attributeName) {
            case "Code":
                return new Code(reader, constantPool);
            case "ConstantValue":
                return new ConstantValue(reader);
            case "Deprecated":
                return new Deprecated(reader.readInt());
            case "Exceptions":
                return new Exceptions(reader);
            case "LineNumberTable":
                return new LineNumberTable(reader);
            case "LocalVariableTable":
                return new LocalVariableTable(reader);
            case "SourceFile":
                return new SourceFile(reader);
            case "Synthetic":
                return new Synthetic(reader.readInt());
            case "InnerClasses":
                return new InnerClasses(reader);
            case "Signature":
                return new Signature(reader);
            case "StackMapTable":
                return new StackMapTable(reader);
            case "RuntimeVisibleAnnotations":
                return new RuntimeVisibleAnnotations(reader);
//            case "BootstrapMethods":
//                return new BootstrapMethods(reader);
            default:
                throw new RuntimeException("unknown attribute info");
        }
    }
}
